/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.adapter.zhuanquanquan;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.xui.widget.edittext.ValidatorEditText;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.entity.ResultAndWeight;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.List;

import butterknife.BindView;

public class DecisionAdapter extends ArrayAdapter<ResultAndWeight> {

    int resourceId;
    List<ResultAndWeight> list;

    public DecisionAdapter(@NonNull Context context, int resource, @NonNull List<ResultAndWeight> objects) {
        super(context, resource, objects);
        resourceId = resource;
        list = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ResultAndWeight item = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        ValidatorEditText resultEdit =(ValidatorEditText)view.findViewById(R.id.edit_result);
        if(!list.get(position).getResult().equals(""))
            resultEdit.setText(list.get(position).getResult());
        ValidatorEditText weightEdit =(ValidatorEditText)view.findViewById(R.id.edit_weight);
        weightEdit.setText(""+list.get(position).getWeight());
        resultEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                list.get(position).setResult(s.toString());
            }
        });
        weightEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                //Toast.makeText(getContext(),s.toString()+"  "+position,Toast.LENGTH_SHORT).show();
                if(!s.toString().equals("")) {
                    if (Integer.parseInt(s.toString()) <= 0) {
                        weightEdit.setText("1");
                        list.get(position).setWeight(1);
                    }
                    else list.get(position).setWeight(Integer.parseInt(s.toString()));
                }
                else {
                    weightEdit.setText("1");
                    list.get(position).setWeight(1);
                }
            }
        });
        ImageButton deleteResult = (ImageButton) view.findViewById(R.id.delete_result);
        deleteResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(list.size()<=2) {
                    ToastUtils.toast("最少2个选项!");
                    return;
                }
                list.remove(position);
                DecisionAdapter.this.notifyDataSetChanged();
            }
        });
        return view;
    }
}
